package com.example.childpositionapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.location.pb.Loc;
import com.example.childpositionapp.model.Location;
import com.example.childpositionapp.model.Login;
import com.example.childpositionapp.model.Receive;
import com.example.childpositionapp.network.Tool;
import com.example.childpositionapp.util.UtilMd5;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChangePwdActivity extends AppCompatActivity {
    EditText et_email;
    EditText et_psw;
    EditText et_yzm;
    Button bt_yzm;
    Button bt_change;
    String yzm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        et_email=findViewById(R.id.et_zh);
        et_psw=findViewById(R.id.et_mm);
        et_yzm=findViewById(R.id.yzm1);
        bt_change=findViewById(R.id.bt_login);
        bt_yzm=findViewById(R.id.bt_yzm);
        et_email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length() < 1) {
                    bt_yzm.setEnabled(false);
                    bt_yzm.setBackgroundResource(R.drawable.shape);
                } else {
                    bt_yzm.setEnabled(true);
                    bt_yzm.setBackgroundResource(R.drawable.shape4);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_yzm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length() < 1) {
                    bt_change.setEnabled(false);
                    bt_change.setBackgroundResource(R.drawable.shape);
                } else {
                    bt_change.setEnabled(true);
                    bt_change.setBackgroundResource(R.drawable.shape4);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        bt_yzm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s=et_email.getText().toString();
                Log.d("33333333",s);
                Tool.getYzm(s, new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ChangePwdActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        Receive location=new Gson().fromJson(response.body().string(), Receive.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (location.getCode()==200){
                                    yzm=location.getMessage();
                                    Toast.makeText(ChangePwdActivity.this,"发送成功",Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(ChangePwdActivity.this,"发送失败",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                });
            }
        });

        bt_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("".equals(et_yzm.getText().toString())) {
                    Toast.makeText(ChangePwdActivity.this, "验证码能为空！", Toast.LENGTH_SHORT).show();
                }else if ("".equals(et_psw.getText().toString())) {
                    Toast.makeText(ChangePwdActivity.this, "密码不能为空！", Toast.LENGTH_SHORT).show();
                }else if (et_yzm.getText().toString().equals(yzm)){
                    String text=new Gson().toJson(new Login(et_email.getText().toString(), UtilMd5.stringMD5(et_psw.getText().toString())));
                    Tool.ChangePsw(text, new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(ChangePwdActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            Location location=new Gson().fromJson(response.body().string(),Location.class);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (location.getCode()==200){
                                        Toast.makeText(ChangePwdActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                                        finish();
                                    }else {
                                        Toast.makeText(ChangePwdActivity.this,location.getMessage(),Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    });
                }else {
                    Toast.makeText(ChangePwdActivity.this,"验证码错误",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

}